## Producing descriptive statistic tables for main outcomes - Appendix Figure A.6 (p. 11)

setwd("~/Replication_PolComm_anon_v2/")

#turn off exponential notation
options(scipen=999)

library(haven)
library(dplyr)
library(knitr)

data <- read_dta("Data/Cleaned/endline_1_merged.dta")

##################################################
#### code to input dvs and produce latex table ###
##################################################

# Define the list of dependent variables
dependent_vars <- c("aff_pol_idx_out", "aff_pol_idx", 
                    "therm_diff", "trust_diff",
                    "comfort_outparty_friends", "comfort_outparty_neighbors", 
                    "comfort_outparty_marriage", "threat_outparty_cit", "threat_outparty_pol",
                    "any_social", "donate_total_any", 
                    "donate_allsides_any", "donate_lrc_any", "donate_ba_any", "answered_amtalks")

# Create an empty data frame to store results
results <- data.frame(variable = character(), mean = numeric(), sd = numeric(), stringsAsFactors = FALSE)

# Loop through each variable to calculate mean and standard deviation
for (var in dependent_vars) {
  stats <- data %>%
    summarise(mean = mean(!!sym(var), na.rm = TRUE),
              sd = sd(!!sym(var), na.rm = TRUE))
  
  results <- rbind(results, data.frame(variable = var, mean = stats$mean, sd = stats$sd, stringsAsFactors = FALSE))
}

# Format the results to three decimal places
results_rounded <- results %>%
  mutate(mean = round(mean, 3), sd = round(sd, 3))

# Print the results as a LaTeX table
kable(results_rounded, format = "latex", booktabs = TRUE, escape = FALSE, caption = "Summary Statistics for Dependent Variables")
